package com.beone.smart.smartBeauty.utils;

import com.beone.smart.smartBeauty.config.TokenInterceptor;
import com.beone.smart.smartBeauty.exception.GlobalException;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Objects;

/**
 * Created by bruce on 20/2/16 -- 15:53
 */
public class RequestUtil {

    public static String getUid() {
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        String uid = (String) request.getAttribute(TokenInterceptor.USER_ID);
        if (StringUtils.isEmpty(uid)) {
            throw new GlobalException("用户id为null");
        }
        return uid;
    }

    public static String getUserName() {
        HttpServletRequest request = ((ServletRequestAttributes) Objects.requireNonNull(RequestContextHolder.getRequestAttributes())).getRequest();
        return (String) request.getAttribute(TokenInterceptor.USER_NAME);
    }
}
